package design.pattern.observer.customer;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * @author futao
 * Created on 2017/11/27 - 19:07.
 */
public class UserAction{
    /**
     * 用来存储所有订阅的对象
     */
    private List<UserActionSubscriber> subscribers = new ArrayList<UserActionSubscriber>();

    /**
     * 订阅，添加订阅用户（观察者），存起来
     * @param subscriber UserActionSubscriber
     */
    public void addSubscriber(UserActionSubscriber subscriber) {
        subscribers.add(subscriber);
    }

    /**
     * 用户登陆成功，去通知所有观察者
     * @param userBean
     */
    public void loginSuccess(UserBean userBean) {
        System.out.println(subscribers.size());
        for (UserActionSubscriber userActionSubscriber:subscribers) {
            //通知每一个订阅者
            userActionSubscriber.onLoginEvent(userBean);
        }
        System.out.println("用户登录了");
    }
    @Test
    public void TestCase001(){
        UserAction userAction=new UserAction();
        DbAdmin dbAdmin=new DbAdmin(userAction);
        userAction.loginSuccess(new UserBean());
    }
}
